<?php
class View {

    public $include_footer = true;
    public $include_header = true;

    /**
     * Class de vue
     */
    public function __construct() {
        
    }

    public function setInclude($header = true, $footer = true) {
        $this->include_header = $header;
        $this->include_footer = $footer;
    }

    /**
     * 
     * @param type $url
     * @param type $params
     */
    public function redirect($path, $params) {
        if (is_array($path)) {
            $action = $path['action'];
            $controller = $path['controller'];
        } elseif (is_string($path)) {
            $action = $path;
        }
        $query = http_build_query($params);

        if (isset($controller)) {
//            header('Location : /' . $controller . '/' . $action . '?' . $query);
        } else {
            header("Location:" . $path . '?'.$query);   
            
            exit;
//            http_redirect($path, $params);
        }
    }

    /**
     * 
     * @param type $path ex /admin/index
     * @param arrat $data 
     */
    public function render($path, $data = array()) {
        if (is_array($path)) {
            $action = $path['action'];
            $controller = $path['controller'];
        } elseif (is_string($path)) {
            $action = $path;
        }
        foreach ($data as $key => $value) {
            $this->$key = $value;
        }
        if ($this->include_header == TRUE)
            require_once APP_PATH . 'views/includes/header.php';

        if (isset($controller)) {
            require_once APP_PATH . "views/" . $controller . "/" . $action . ".phtml";
        } else {
            require_once APP_PATH . "views/" . $path . ".phtml";
        }

        if ($this->include_footer == TRUE)
            require_once APP_PATH . 'views/includes/footer.php';
    }

    public function renderPartial($path, $data = array()) {
        if (is_array($path)) {
            $action = $path['action'];
            $controller = $path['controller'];
        } elseif (is_string($path)) {
            $action = $path;
        }

        foreach ($data as $key => $value) {
            $this->$key = $value;
        }

        if (isset($controller)) {
            require_once APP_PATH . "views/" . $controller . "/" . $action . ".phtml";
        } else {
            require_once APP_PATH . "views/" . $action . ".phtml";
        }
    }

}